


// 商品服务实现扩展
package com.neighbor.neighborhoodadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neighbor.neighborhoodadmin.dao.IMallProductsDao;
import com.neighbor.neighborhoodadmin.pojo.MallProducts;
import com.neighbor.neighborhoodadmin.service.IMallProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 积分商城商品表 服务实现类
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */


@Service
public class MallProductsServiceImpl extends ServiceImpl<IMallProductsDao, MallProducts>
        implements IMallProductsService {

    @Autowired
    private IMallProductsDao productsDao;

    @Override
    public List<MallProducts> getProductsByType(Integer typeId) {
        return productsDao.findActiveProductsByType(typeId);
    }

    @Override
    public List<MallProducts> getAvailableProducts() {
        return productsDao.findAvailableProducts();
    }

    @Override
    @Transactional
    public boolean reduceStock(Integer productId, Integer quantity) {
        UpdateWrapper<MallProducts> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("stock = stock - " + quantity)
                .eq("product_id", productId)
                .ge("stock", quantity);
        return this.update(updateWrapper);
    }
}